/***************************************************************************
 *   Copyright (C) 2007 by Grzegorz Latarowski, Maciej Gajewski            *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/



// stdlib
#include <sys/time.h>	// gettimeofday

// Qt
#include <QSettings>
#include <QDebug>

// libautonet
#include "autopackets.h"

// local
#include "eventsenderwindow.h"



uint64_t get_time()
{
	struct timeval t;
	gettimeofday( & t, NULL );
	return (uint64_t)t.tv_sec * 1000 + t.tv_usec / 1000;
}



// ---------------- EventSenderWindow::EventSenderWindow() -------------------
EventSenderWindow::EventSenderWindow( QWidget * pParent, Qt::WindowFlags f )
	: QWidget( pParent, f )
{
	setupUi( this );
	
		// register packets 
	GAutoPacket::registerPackets();
	
		// grab recent receiverAddress from settings and insert then into comboBox
	QSettings settings;
	QStringList hosts = settings.value( "receiverAddress" ).toStringList();
	if( comboHostAddress )
	{
		comboHostAddress->addItems( hosts );
	}
	int lastPort = settings.value( "lastPort", 34445 ).toInt();
	if( spinHostPort )
	{
		spinHostPort->setValue( lastPort );
	}
}
// ---------------------------------------------------------------------------


// ---------------- EventSenderWindow::~EventSenderWindow() ------------------
EventSenderWindow::~EventSenderWindow()
{
	QSettings settings;
	
		// save receiverAddress in settings
	if( comboHostAddress )
	{
			// get recent hosts list
		QStringList hosts;
		for( int i = 0; i < comboHostAddress->count(); i++ )
		{
			hosts << comboHostAddress->itemText( i );
		}
			// save list
		settings.setValue( "receiverAddress", QVariant( hosts ) );
	}
	
	if( spinHostPort )
	{
		settings.setValue( "lastPort", QVariant( spinHostPort->value() ) );
	}
}
// ---------------------------------------------------------------------------


// -------------------- EventSenderWindow::sendUdpPacket() -------------------
void EventSenderWindow::sendUdpPacket( const GAutoPacket& packet,
									   const QHostAddress& address,
									   quint16 port
									 )
{
	QByteArray buffer;
	QDataStream stream( &buffer, QIODevice::WriteOnly );
	
	GHeader header = packet.header();
	
	stream << header << packet;
	
	_udpSender.sendSync( buffer, address, port );
}
// ---------------------------------------------------------------------------


// --------------- EventSenderWindow::on_toolSendEvent_clicked() -------------
void EventSenderWindow::on_toolSendEvent_clicked()
{
		// get event type from events combobox
	quint16 eventType = comboEventType->currentIndex();
	GAutoEventTrigger event( (GAutoEventTrigger::EventType) eventType );

		// set events message
	event.setMessage( lineEventMessage->text() );
	uint64_t timestamp = get_time();
	event.setSendTime( timestamp );
	if( spinShutterDelay )
	{
		event.setShutterTime( timestamp + spinShutterDelay->value() );
	}

	QHostAddress address( comboHostAddress->currentText() );
	quint16 port = spinHostPort->value();
	sendUdpPacket( event, address, port );
}
// ---------------------------------------------------------------------------







// eof
